package pl.polidea.imf.blackberry.aspects;

//import org.aspectme.api.MethodInterceptor;
//import org.aspectme.api.MethodInvocation;
//import org.aspectme.cldc.lang.Void;
//
//import pl.polidea.imf.blackberry.service.IMFService;
//import pl.polidea.imf.common.java.log.LogBuilder;
//import pl.polidea.imf.common.java.model.LogItem;
//import pl.polidea.imf.common.java.util.ApplicationInfoProvider;
//
//public class LoggingAdvice implements MethodInterceptor {
//
//	public Object invoke(MethodInvocation invocation) throws Throwable {
//		String methodSign = getMethodSignature(invocation);
//		IMFService.collectLog(new LogBuilder(LogItem.LEVEL.INFO,LogItem.TYPE.METHOD,"Enter " + methodSign,ApplicationInfoProvider.getRevision(),System.currentTimeMillis()).build());
//		System.out.println("Enter " + methodSign);
//		try {
//			// Proceed to the "real" method invocation (or another advice)
//			Object result = invocation.proceed();
//			if (invocation.getMethod().getReturnType().equals(Void.class)) {
//				IMFService.collectLog(new LogBuilder(LogItem.LEVEL.INFO,LogItem.TYPE.METHOD,"Exit " + methodSign,ApplicationInfoProvider.getRevision(),System.currentTimeMillis()).build());
//				System.out.println("Exit " + methodSign);
//			} else {
//				IMFService.collectLog(new LogBuilder(LogItem.LEVEL.INFO,LogItem.TYPE.METHOD,"Exit " + methodSign + " with return value " + result,ApplicationInfoProvider.getRevision(),System.currentTimeMillis()).build());
//				System.out.println("Exit " + methodSign + " with return value " + result);
//			}
//			return result;
//		} catch (Exception e) {
//			IMFService.collectLog(new LogBuilder(LogItem.LEVEL.INFO,LogItem.TYPE.METHOD,"Exit " + methodSign + " with exception " + e ,ApplicationInfoProvider.getRevision(),System.currentTimeMillis()).build());
//			System.out.println("Exit " + methodSign + " with exception " + e);
//			throw e;
//		}
//	}
//	
//	
//	/**
//	 * Assembles a pretty formatted string with all parameter values.
//	 */
//	private String getMethodSignature(MethodInvocation invocation) {
//		StringBuffer sign = new StringBuffer(invocation.getMethod().getName());
//		sign.append('(');
//		Class[] paramTypes = invocation.getMethod().getParameterTypes();
//		Object[] args = invocation.getArguments();
//		for (int i = 0; i < args.length; i++) {
//			if (i > 0) {
//				sign.append(", ");
//			}
//			Class type = paramTypes[i];
//			Object arg = args[i];
//			appendValue(sign, type, arg);
//		}
//		sign.append(')');
//		return sign.toString();
//	}
//	
//	/**
//	 * Make strings and chars look pretty. Also appends all elements in object arrays.
//	 */
//	private void appendValue(StringBuffer buf, Class type, Object arg) {
//		// Make strings and chars look pretty
//		if (type.equals(String.class) && arg != null) {
//			buf.append('\"');
//			buf.append(arg);
//			buf.append('\"');
//		} else if (type.equals(Character.class)) {
//			buf.append('\'');
//			buf.append(arg);
//			buf.append('\'');
//		} else if (type.equals(Object[].class) && arg != null) {
//			buf.append('[');
//			Object[] elems = (Object[]) arg;
//			for (int i = 0; i < elems.length; i++) {
//				Object elem = elems[i];
//				Class elemType = Object.class;
//				if (elem != null) {
//					elemType = elem.getClass();
//				}
//				appendValue(buf, elemType, elem);
//			}
//			buf.append(']');
//		} else {
//			buf.append(arg);
//		}
//	}
//	
//}
